package com.borun.dictionary;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.borun.bean.Base;
import com.borun.dao.BaseDao;

public class AddBase extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private BaseDao dao = new BaseDao();
	private JLabel lblBaseName;
	private JTextField txtBaseName;
	private JButton btnAdd,btnCancel;
	
	public AddBase(){
		
		super("添加词库");
		this.setLayout(null);
		
		lblBaseName = new JLabel("词库名称：");
		lblBaseName.setBounds(30, 40, 100, 30);
		
		txtBaseName = new JTextField();
		txtBaseName.setBounds(100, 40, 160, 30);
				
		btnAdd = new JButton("添加");
		btnAdd.addActionListener(this);
		btnAdd.setBounds(50,120,80,30);
		
		btnCancel = new JButton("取消");
		btnCancel.addActionListener(this);
		btnCancel.setBounds(160,120,80,30);
		
		this.add(lblBaseName);
		this.add(txtBaseName);
		this.add(btnAdd);
		this.add(btnCancel);
		
		
		this.setSize(300, 200);
		this.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置
		this.setResizable(false); // 禁止改变窗体大小
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
//	public static void main(String[] args) {
//		new AddBase();
//	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnAdd){
			String basename = txtBaseName.getText();
			if("".equals(basename) || basename.trim().isEmpty()){
				JOptionPane.showMessageDialog(null, "请正确填写词库名称！");
				return;
			}
			Base base = new Base(basename);
			try {
				if(dao.addBase(base)){
					JOptionPane.showMessageDialog(null, "添加成功！下次启动时生效");
					this.setVisible(false);
				}else{
					JOptionPane.showMessageDialog(null, "系统异常，添加失败！");
				}
			} catch (HeadlessException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}else if(e.getSource() == btnCancel){
			this.setVisible(false);
		}
	}

}
